package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by 朱城委 on 2019/7/9.<br><br>
 *
 * 中等：三数之和
 */
public class Topic15 {

    public static void main(String[] args) {
        BaseSolution solution = new Solution2();

        int[] array1 = {-1, 0, 1, 2, -1, -4};
        int[] array2 = {-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6};
        int[] array3 = {-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0};
        int[] array4 = {0, 0, 0, 0};
        int[] array5 = {0, 0, 0};

        System.out.println(solution.threeSum(array1));
        System.out.println(solution.threeSum(array2));
        System.out.println(solution.threeSum(array3));
        System.out.println(solution.threeSum(array4));
        System.out.println(solution.threeSum(array5));
    }

    private interface BaseSolution {
        List<List<Integer>> threeSum(int[] nums);
    }

    private static class Solution1 implements BaseSolution {

        @Override
        public List<List<Integer>> threeSum(int[] nums) {
            if(nums.length == 0) {
                return new ArrayList<>();
            }

            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
            for(int i = 0; i < nums.length; i++) {
                if(i > 0 && nums[i - 1] == nums[i]) {
                    continue;
                }

                int low = i + 1;
                int high = nums.length - 1;
                while (low < high) {
                    int sum = nums[i] + nums[low] + nums[high];
                    if(sum == 0) {
                        result.add(Arrays.asList(nums[i], nums[low], nums[high]));

                        while (low < high && nums[low + 1] == nums[low]) {
                            low++;
                        }

                        while (low < high && nums[high - 1] == nums[high]) {
                            high--;
                        }

                        low++;
                        high--;
                    }
                    else if(sum < 0) {
                        low++;
                    }
                    else {
                        high--;
                    }
                }
            }

            return result;
        }
    }

    /**
     * 解法超时
     */
    private static class Solution2 implements BaseSolution {

        @Override
        public List<List<Integer>> threeSum(int[] nums) {
            if(nums == null || nums.length < 3) {
                return new ArrayList<>();
            }

            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
            for(int i = 0; i < nums.length - 2; i++) {
                if(i > 0 && nums[i - 1] == nums[i]) {
                    continue;
                }

                for(int j = i + 1; j < nums.length - 1; j++) {
                    if(j > i + 1 && nums[j - 1] == nums[j]) {
                        continue;
                    }

                    for(int k = j + 1; k < nums.length; k++) {
                        if(k > j + 1 && nums[k - 1] == nums[k]) {
                            continue;
                        }

                        if(nums[i] + nums[j] + nums[k] == 0) {
                            result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                        }
                    }
                }
            }

            return result;
        }
    }
}
